home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Essentials / C++ A'Link Files / 1990 / Sep 90 / CPlus.Dev$ 9⁄14⁄90 / 0200-Assignment construct-Sep90 < prev    next >
Encoding:
Text File  |  1991-03-06  |  3.3 KB  |  96 lines  |  [TEXT/GEOL]

  1. Item forwarded  by  MM.XOBJ      to D0776        D5543        D3431
  2.  
  3. Item    3035708                         12-Sept-90        12:41PDT
  4.  
  5. From:   MM.XOBJ                         MacroMind, XObject Support,PRT
  6.  
  7. To:     TIM.SWIHART                     Swihart, Tim
  8.         CPLUS.DEV$                      C++ Interest List--Developers
  9.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  10.  
  11. Sub:    Assignment constructor bug?
  12.  
  13. Can anyone explain this problem, is it a bug or a feature?  I spent a day
  14. tracking down why a file would not compile.  I managed to boil it down to its
  15. purest form.
  16.  
  17. Given a function, funcThatTakesAFoo(Foo f), that is passed a Foo by value,
  18. passing it an object of the derived class, Bar, is also fine.  I do this in two
  19. different contexts:
  20.  
  21.     class Foo { /* .... */ };
  22.     class Bar : public Foo { /* .... */ };
  23.  
  24.     extern void funcThatTakesAFoo(Foo);
  25.  
  26.     void func1()
  27.     {
  28.           Bar b;
  29.  
  30.            funcThatTakesAFoo(b);   // this of course, is fine
  31.     }
  32.  
  33.     void func2(Bar b)
  34.     {
  35.           funcThatTakesAFoo(b);
  36.     }
  37.  
  38.  
  39. With no constructors or default constructors for Foo and Bar, both of these
  40. functions are fine.  If I add a constructor for the derived class of the form
  41. Bar::Bar(Bar &), however, func1() is still OK, but func2() yields the error:
  42.  
  43.     # error: cannot cast class object to pointer
  44.  
  45. This error is also generated by the expression:
  46.  
  47.     Foo f = Bar b;
  48.  
  49. The problem goes away if an assignment constructor styled Foo::Foo(&Foo)
  50. exists.  (Thank you Al McNeil for saving my life!)
  51.  
  52. Now I know that providing an explicit operator= or assignment constructor
  53. requires *derived* classes to provide equivalent constructs, since the default
  54. memberwise assignment is defeated from that point forward.  What I don't
  55. understand is why it seems to be defeated from that point backwards to Foo!
  56.  
  57. I also don't understand why these two functions which generate nearly identical
  58. code (the local Bar versus the parameter Bar is of little consequence) stop
  59. compiling similarly with the introduction of Bar::Bar(Bar&).  I have looked at
  60. the generated output of the successful compiles. Alas, CFront won't generate a
  61. single line of C for the function if it fails to compile.
  62.  
  63. It all boils down to two problems:
  64.  
  65. 1) the existence of Bar::Bar(Bar &) makes “Foo f = Bar b” illegal.
  66.  
  67. 2) This construct is generated only when I pass a parameter received by value
  68. to a function that takes its base class by value, when the constructor
  69. Bar::Bar(Bar &) exists, and not when it doesn't.  Why is a temporary variable
  70. necessary for this.
  71.  
  72. Fortunately, I own the source code to my equivalent of class Foo, and have
  73. provided the workaround, but I don't know what I would have done if this were a
  74. library, and I resent having to change an otherwise stable class.
  75.  
  76. If this is specified in the language somehow, and if someone can point me to
  77. the appropriate spot in the AT&T manual, or Lippman, or even the original
  78. Stroustrup, I would be grateful.  (The Annotated C++ Reference seems hard to
  79. come by on the East coast).
  80.  
  81. If it is some sort of bug, I would like to know if it is located in CFront or
  82. subsequent Apple adaptation.  There is no reason I know of that using the
  83. following C output should cease to be correct because of Bar::Bar(Bar &).
  84.  
  85.     struct Foo f;
  86.     struct Bar b;
  87.  
  88.     f = b;
  89.  
  90. Haim Zamir
  91. MacroMind, Inc.
  92. AppleLink MM.XOBJ
  93.  
  94.  
  95.  
  96.